/**
 * @file    yd1363_data.c
 * @brief   ydt1363数据链路层
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-05-16
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-05-16 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "yd1363_data.h"
#include "mcu_type.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief
 * @param
 * @return
*/
uint16_t yd1636_calc_crc(uint8_t *data_buf , uint16_t data_len)
{
    uint32_t sum = 0;
    uint16_t i = 0;
    uint16_t crc_code = 0;
    for(i = 0;i < data_len; ++ i)
    {
        sum += data_buf[i];
    }
    crc_code = (~(sum & 0xffff) + 1);
    return crc_code;
}

void byte_to_ascii(uint8_t byte ,uint8_t *p_buf)
{
    uint8_t bh_8 = 0;
    uint8_t bl_8 = 0;

    bh_8 = (byte >> 4) & 0x0f;
    bl_8 = byte & 0x0f;

    p_buf[0] = bh_8 <= 9 ? (0x30 + bh_8):(0x37 + bh_8);
    p_buf[1] = bl_8 <= 9 ? (0x30 + bl_8):(0x37 + bl_8);
}

void half_to_ascii(uint16_t half ,uint8_t *p_buf)
{
    uint8_t i = 0;
    uint8_t vtemp = 0;
    for( i = 0;i < 4; ++ i)
    {
        vtemp = half & 0x0f;
        if(vtemp <= 9)
            p_buf[3 - i] = 0x30 + vtemp;
        else
            p_buf[3 - i] = 0x37 + vtemp;
        
        half >>= 4;
    }
}

void word_to_ascii(uint32_t word ,uint8_t *p_buf)
{
    uint8_t i = 0;
    uint8_t vtemp = 0;
    for( i = 0;i < 8; ++ i)
    {
        vtemp = word & 0x0f;
        if(vtemp <= 9)
            p_buf[7 - i] = 0x30 + vtemp;
        else
            p_buf[7 - i] = 0x37 + vtemp;
        
        word >>= 4;
    }
}

void str_to_ascii(uint8_t *str , uint16_t len , uint8_t *p_buf)
{
    uint16_t i = 0;
    for( i = 0;i < len; ++ i)
    {
        byte_to_ascii(str[i],p_buf);
        p_buf += 2;
    }
}

uint8_t ascii_to_byte(uint8_t *p_buf)
{
    uint8_t bh_8 = p_buf[0];
    uint8_t bl_8 = p_buf[1];
    uint8_t byte = 0;

    if(bh_8 > '9')
        bh_8 -= 'A' - 10;
    else
        bh_8 = bh_8 - 0x30;

    bh_8 &= 0x0f;    

    if(bl_8 > '9')
        bl_8 -= 'A' - 10;
    else
        bl_8 = bl_8 - 0x30;
    bl_8 &= 0x0f; 

    byte = (bh_8 << 4) + bl_8;
    return byte;
}

uint16_t ascii_to_half_word(uint8_t *p_buf)
{
    uint16_t half_data = 0;

    half_data = ascii_to_byte(p_buf) << 8;
    half_data |= ascii_to_byte(p_buf + 2);

    return half_data;
}

uint32_t ascii_to_word(uint8_t *p_buf)
{
    uint32_t word_data = 0;

    word_data = ascii_to_byte(p_buf) << 24;
    word_data |= ascii_to_byte(p_buf + 2) << 16;
    word_data |= ascii_to_byte(p_buf + 4) << 8;
    word_data |= ascii_to_byte(p_buf + 6);

    return word_data;
}

uint8_t check_len_vaild(uint16_t recv_len)
{
    uint8_t sum = 0;
    sum = ((recv_len & 0x0f00) >> 8) + ((recv_len & 0x0f0) >> 4) + (recv_len & 0x0f);
    sum = (~sum + 1) & 0x0f;
    if(sum == ((recv_len >> 12) & 0x0f))
        return TRUE;
    else
        return FALSE;
}

uint16_t yd1363_calc_len(uint16_t len)
{
    uint8_t sum = 0;
    
    sum = ((len & 0x0f00) >> 8) + ((len & 0x0f0) >> 4) + (len & 0x0f);
    sum = (~sum + 1) & 0x0f;
    
    return ((len & 0x0fff) | ((sum << 12) & 0xf000));
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


